package com.xsomnus.java_basic.juc.threadlocal;

/**
 * @author 叶小夏
 * @since 2019-08-23 14:17
 */
public class ThreadLocalTest {

    static void print(String str) {
        // 1. 打印当前线程本地内存中的localVar变量的值
        System.out.println(str + ":" + localVar.get());
        // 2. 清除当前线程本地内存中localVar变量
        localVar.remove();
    }


    static ThreadLocal<String> localVar = new ThreadLocal<>();

    public static void main(String[] args) {

        Thread threadOne = new Thread(() -> {
            localVar.set("threadOne local var");
            print("threadOne");
            System.out.println("threadOne remove after" + ":" + localVar.get());
        });

        Thread threadTwo = new Thread(() -> {
            localVar.set("threadTwo local var");
            localVar.set("threadTwo local var2");
            print("threadTwo");
            System.out.println("threadTwo remove after" + ":" + localVar.get());
        });


        threadOne.start();
        threadTwo.start();
    }

}
